package com.xinyues.gamemanager.service;

import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.xinyues.gamemanager.common.GmBusinessException;
import com.xinyues.gamemanager.common.GmConstants;
import com.xinyues.gamemanager.common.GmDateUtils;
import com.xinyues.gamemanager.dao.GmAccountRepository;
import com.xinyues.gamemanager.dao.entity.GmAccount;
import com.xinyues.gamemanager.params.ChangeAccountRole;

@Service
public class GmAccountService {

    @Autowired
    private GmAccountRepository gmAccountRepository;

    public GmAccount getGmAccount(String username, String password) {
        GmAccount gmAccount = gmAccountRepository.findByUsernameAndPassword(username, password);
        return gmAccount;
    }

    public GmAccount getGmAccount(String username) {
        GmAccount gmAccount = gmAccountRepository.findByUsername(username);
        return gmAccount;
    }

    public void deleteAccount(long accountId) {
        gmAccountRepository.deleteById(accountId);
    }

    public void addGmAccount(String username, String password, String creator) {
        if (username.equals(GmConstants.ADMIN)) {
            throw new GmBusinessException("不可以创建admin账号");
        }
        GmAccount gmAccount = this.getGmAccount(username);
        if (gmAccount != null) {
            gmAccount.setPassword(password);
            gmAccount.setCreator(creator);

        } else {
            gmAccount = new GmAccount();
            gmAccount.setCreateTime(GmDateUtils.yyyyMMddHHmmss());
            gmAccount.setCreator(creator);
            gmAccount.setId(System.currentTimeMillis());
            gmAccount.setUsername(username);
            gmAccount.setPassword(password);
        }
        gmAccountRepository.save(gmAccount);
    }
   public GmAccount getCurrentAccount() {
       Object value = SecurityContextHolder.getContext().getAuthentication().getDetails();
       if(value == null) {
           return null;
       }
       return (GmAccount)value;
   }
    
    

    public List<GmAccount> getAllGmAccount() {
        return gmAccountRepository.findAll();
    }

    public void updateAccountRole(ChangeAccountRole changeAccountRole) {
        GmAccount account = this.gmAccountRepository.findById(changeAccountRole.getUserId()).orElse(null);
        if (account != null) {
            account.getRoles().clear();// 目前只支持一种角色
            if (StringUtils.isNoneBlank(changeAccountRole.getRoleId())) {
                account.getRoles().add(changeAccountRole.getRoleId());
            }
            this.gmAccountRepository.save(account);
        }
    }

    public boolean updateAccountRole(GmAccount account) {
        if (account.getId() != 0) {
            this.gmAccountRepository.save(account);
            return true;
        } 
        return false;
    }
}
